/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package my.algorithms.mcorrea;

/**
 *
 * @author mcorrea
 */
public class GoogleMain {
	
	public static void main(String args[]){
		
		int[] a1 = {1, 2};
		int[] a2 = {1, 3};
		arraymul(a1, a2 );
		
		System.out.println(  "\n----------------------------" );
		String s1 = "skjthshetheshetesm";
		String s2 = "she";
		removeSubString(s1, s2);
		
		System.out.println(  "\n----------------------------" );
		s1 = "abcdrrrrrrrrrrabcdrrrrrrrrrrrrrrrrrrrrrrrabcdrrrrrrrrrrrrabcdsssss";
		s2 = "abcd";
		removeSubString(s1, s2);
		
		
		System.out.println(  "\n----------------------------" );
		int a3[] ={-12, -7, -4, 0, 3, 5, 9, 10, 15, 16};
		findpairssum(a3);
		
		
	
	}
	
	
	/**
	 * Two very large numbers are represented using arrays. Multiply these two numbers. E.g. Two numbers 12 and 13 are represented as a=[1,2] adn b=[1,3]. The expected result is 12*13=156 i.e. c=[1,5,6]
	 * @param a1
	 * @param a2 
	 */
	public static void arraymul(int[] a1, int a2[]){
		
		int[] result = new int[ 3 ];
		int k = 0 ;
		
		for(int i=0; i<a1.length; i++){
			
			for(int j=0; j<a2.length; j++){
				result[k] += a2[j]*a1[i];
				k++;
			}
			k = i+1;
		
		}
		
		
		for(int r: result)
			System.out.print(r + " ");
		
	
	}
	
	
	/**
	 * Given string input1, input2, remove wherever the occurrence of input2 in input1.
		e.g:
		input1: skjth she the she tesm
		input2: she
		input1 will become "skjththetesm"
		Give the test cases.
	 * @param s1
	 * @param s2 
	 */
	public static void removeSubString(String s1, String s2){
		String result = "";
		while( s1.indexOf(s2) != -1   ){
			
			result += s1.substring( 0 , s1.indexOf(s2)   );
			s1 = s1.substring(   s1.indexOf(s2)+s2.length() , s1.length()  );
			
			//remaining of the string
			if( s1.indexOf(s2) == -1 ){
				result += s1;
			}
			
		
		}
		
		System.out.println(  result  );
	
	}
	
	
	
	/**
	 * Given a sorted array, output all triplets <a,b,c> such that a-b = c.
	 * Expected time is O(n^2). My approach using binary search took O(n^2 logn).
	 * When you attempt an approach, test your code with this example and 
	 * list your outputs for verification. 
	 * -12, -7, -4, 0, 3, 5, 9, 10, 15, 16
	 * @param a1 
	 */
	public static void findpairssum(int[] array){
		
		for(int n: array){
			//int n = 1; //the sum is 1
		
			int low = 0;
			int high = array.length - 1;
			int sum = 0;

			while(low<array.length && high>0 ){
				
				sum = array[low] + array[high];
				
				if(sum == n){
					System.out.println(n+" - "+array[high] +" = "+array[low]);
					high--;
					low++;
				}else if(sum > n){
					high--;
				}else{
					low++;
				}
			}
		}

	
	}
	
	
	
}

